home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bflpop.c < prev    next >
Text File  |  1989-12-28  |  3KB  |  118 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bflpop.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bflpop - pop block off of free list
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bflpop(bp, bn_p)
  18.      BLKFILE *bp;
  19.      bpos_t *bn_p;
  20.  
  21. DESCRIPTION
  22.      The bflpop function gets a block out of the free list of the
  23.      block file associated with BLKFILE pointer bp.  If the free list
  24.      is empty, the block number of the first block past the end of
  25.      file is retrieved.  The block number of the new block is returned
  26.      in the location pointed to by bn_p.
  27.  
  28.      Note that if the free list is empty, the same end block will be
  29.      retrieved by flpop until that block is written, extending the
  30.      file length.
  31.  
  32.      bflpop will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       bp is not a valid BLKFILE pointer.
  35.      [EINVAL]       bn_p is the NULL pointer.
  36.      [BEEOF]        bp is empty.
  37.      [BEEOF]        Free list head past points past the
  38.                     end of the file.
  39.      [BENFL]        bp does not have a free list.
  40.      [BENOPEN]      bp is not open for writing.
  41.  
  42. SEE ALSO
  43.      bflpush.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise,
  47.      a value of -1 is returned, and errno set to indicate the error.
  48.  
  49. NOTES
  50.      To use the free list functions in the blkio library, the first
  51.      element of the file header must be the free list head with type
  52.      bpos_t.  This must be initialized to 0 immediately after the file
  53.      is created and not accessed afterward except using bflpop and
  54.      bflpush.  Also, the block size must be at least sizeof(bpos_t).
  55.  
  56. ------------------------------------------------------------------------------*/
  57. int bflpop(bp, bn_p)
  58. BLKFILE *bp;
  59. bpos_t *bn_p;
  60. {
  61.     bpos_t oldflhno = 0;
  62.     bpos_t newflhno = 0;
  63.  
  64.     /* validate arguments */
  65.     if (!b_valid(bp) || (bn_p == NULL)) {
  66.         errno = EINVAL;
  67.         return -1;
  68.     }
  69.  
  70.     /* check if not open for writing */
  71.     if (!(bp->flags & BIOWRITE)) {
  72.         errno = BENOPEN;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if no free list */
  77.     if ((bp->hdrsize < sizeof(oldflhno)) || (bp->blksize < sizeof(oldflhno))) {
  78.         errno = BENFL;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if header not yet written */
  83.     if (bp->endblk < 1) {
  84.         errno = BEEOF;
  85.         return -1;
  86.     }
  87.  
  88.     /* get block number of current free list head */
  89.     if (bgethf(bp, (size_t)0, &oldflhno, sizeof(oldflhno)) == -1) {
  90.         BEPRINT;
  91.         return -1;
  92.     }
  93.     if (oldflhno >= bp->endblk) {
  94.         errno = BEEOF;
  95.         return -1;
  96.     }
  97.  
  98.     /* if free list empty, get new block at end of file */
  99.     if (oldflhno == 0) {
  100.         oldflhno = bp->endblk;
  101.     } else {    /* else get new free list head */
  102.         if (bgetbf(bp, oldflhno, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  103.             BEPRINT;
  104.             return -1;
  105.         }
  106.         if (bputhf(bp, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  107.             BEPRINT;
  108.             return -1;
  109.         }
  110.     }
  111.  
  112.     /* load return argument */
  113.     *bn_p = oldflhno;
  114.  
  115.     errno = 0;
  116.     return 0;
  117. }
  118.